package org.daidai.abstrusewriting.homepage.classify.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.classify.activitys.ClassifyItemActivity;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyItem;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyList;

import java.util.Collection;
import java.util.List;
public class ClassifyAdapter extends BaseAdapter{
    private static final String TAG = "ClassifyAdapter";

    private List<ClassifyList> mList;
    private Context mContext;

    public ClassifyAdapter(List<ClassifyList> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }


    @Override
    public int getCount() {
        int ret=0;

        if (mList != null) {
            ret=mList.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {

        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addAll(Collection<? extends ClassifyList> collection){
         mList.clear();
         mList.addAll(collection);
         Log.d(TAG, "addAll: "+mList.toString());
         notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView= LayoutInflater.from(mContext)
                                       .inflate(R.layout.classify_item,parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }

        ViewHolder holder = (ViewHolder)convertView.getTag();
        holder.bindView(position);

        return convertView;
    }

    private class ViewHolder{
       private ImageView cover0;
       private TextView title0;
       private CardView mCardView0;


       private ImageView cover1;
       private TextView title1;
        private CardView mCardView1;

       private ImageView cover2;
       private TextView title2;
        private CardView mCardView2;

        private ImageView cover3;
        private TextView title3;
        private CardView mCardView3;

        private ImageView cover4;
        private TextView title4;
        private CardView mCardView4;

        private ImageView cover5;
        private TextView title5;
        private CardView mCardView5;

        public ViewHolder(View itemView){
           title0=((TextView)itemView.findViewById(R.id.classify_item_txt0));
           cover0=((ImageView)itemView.findViewById(R.id.classify_item_cover0));
           mCardView0= (CardView) itemView.findViewById(R.id.classify_item_cardview0);

           title1=((TextView)itemView.findViewById(R.id.classify_item_txt1));
           cover1=((ImageView)itemView.findViewById(R.id.classify_item_cover1));
            mCardView1= (CardView) itemView.findViewById(R.id.classify_item_cardview1);

           title2=((TextView)itemView.findViewById(R.id.classify_item_txt2));
           cover2=((ImageView)itemView.findViewById(R.id.classify_item_cover2));
           mCardView2= (CardView) itemView.findViewById(R.id.classify_item_cardview2);

           title3=((TextView)itemView.findViewById(R.id.classify_item_txt3));
           cover3=((ImageView)itemView.findViewById(R.id.classify_item_cover3));
           mCardView3= (CardView) itemView.findViewById(R.id.classify_item_cardview3);

           title4=((TextView)itemView.findViewById(R.id.classify_item_txt4));
           cover4=((ImageView)itemView.findViewById(R.id.classify_item_cover4));
           mCardView4= (CardView) itemView.findViewById(R.id.classify_item_cardview4);

           title5=((TextView)itemView.findViewById(R.id.classify_item_txt5));
           cover5=((ImageView)itemView.findViewById(R.id.classify_item_cover5));
           mCardView5= (CardView) itemView.findViewById(R.id.classify_item_cardview5);

       }

        private void bindView(int position){
            if (mList != null) {
                if (mList.get(position) != null) {
                    ClassifyList classifyList= mList.get(position);

                    for (int i = 0; i <classifyList.getClassifyItems().size()  ; i++) {
                       final ClassifyItem item = classifyList.getClassifyItems().get(i);

                       switch (i){
                           case 0:
                               title0.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover0);
                               mCardView0.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);


                                           }
                                       }
                               );
                               break;
                           case 1:
                               title1.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover1);
                               mCardView1.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);
                                           }
                                       }
                               );
                               break;
                           case 2:
                               title2.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover2);
                               mCardView2.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);
                                           }
                                       }
                               );
                               break;
                           case 3:
                               title3.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover3);
                               mCardView3.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);
                                           }
                                       }
                               );
                               break;
                           case 4:
                               title4.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover4);
                               mCardView4.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);
                                           }
                                       }
                               );

                               break;
                           case 5:
                               title5.setText(item.getTitle());
                               Picasso.with(mContext)
                                       .load(item.getCoverPath())
                                       .config(Bitmap.Config.RGB_565)
                                       .resize(40,40)
                                       .into(cover5);
                               mCardView5.setOnClickListener(
                                       new View.OnClickListener() {
                                           @Override
                                           public void onClick(View v) {
                                               Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT).show();
                                               Intent intent = new Intent(mContext, ClassifyItemActivity.class);
                                               intent.putExtra("item",item);
                                               mContext.startActivity(intent);
                                           }
                                       }
                               );
                               break;
                       }



                   }

                }

            }

        }

    }
}
